package qinggui.mlz.app.controller.category;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import qinggui.mlz.app.domain.category.CategoryDetailVO;
import qinggui.mlz.app.domain.category.CategoryListVO;
import qinggui.mlz.module.category.entity.Category;
import qinggui.mlz.module.category.entity.dto.CategoryDTO;
import qinggui.mlz.module.category.mapper.CategoryMapper;
import qinggui.mlz.module.category.service.CategoryService;
import qinggui.mlz.module.utils.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 类目表 前端控制器
 * </p>
 *
 * @author 清规
 * @since 2025-02-15
 */
@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 获取所有类目
     *
     * @param keyword 类目搜索关键字，可为空
     * @return Response<List<CategoryListVO>> 包含类目列表的响应对象
     */
    @RequestMapping("/category/list")
    public Response getAll(@RequestParam(required = false) String keyword) {
        List<Category> allList = categoryService.getAllParentCategory(keyword);
        if (allList == null) {
            return new Response<>(4004,"类目不存在");
        }
        List<CategoryListVO> categoryVOS = new ArrayList<>();
        for (Category category : allList) {
            CategoryListVO categoryVO = new CategoryListVO();
            // 根据父类目id获取子类目
            List<Category> childCategory = categoryMapper.getCategoryByParentId(category.getId());
            // 将childCategory中的属性赋值到categoryListVO中
            List<CategoryListVO> categoryListVOS = new ArrayList<>();
            childCategory.forEach(child -> {
                CategoryListVO categoryListVO = new CategoryListVO().setId(child.getId()).setCategoryName(child.getCategoryName());
                categoryListVOS.add(categoryListVO);
            });
            categoryVO.setCategoryName(category.getCategoryName())
                    .setCategoryImage(category.getCategoryImage())
                    .setChildCategory(categoryListVOS);
            categoryVOS.add(categoryVO);
        }
        return new Response<>(1001,categoryVOS);
    }

    /**
     * 获取类目详情
     *
     * @param id 类目id
     * @return CategoryListVO
     */
    @RequestMapping("/category/detail")
    public Response getById(Long id) {
        Category category;
        try {
            category = categoryService.getById(id);
            if (category == null) {
                return new Response(4004,"类目不存在");
            }
        } catch (Exception e) {
            return new Response(4004,"类目不存在");
        }
        CategoryDetailVO categoryDetailVO = new CategoryDetailVO().setId(id)
                .setCategoryName(category.getCategoryName())
                .setCategoryImage(category.getCategoryImage())
                .setCreateTime(category.getCreateTime())
                .setUpdateTime(category.getUpdateTime());
        return new Response<>(1001,categoryDetailVO);
    }

    /**
     * 获取类目树
     * @param categoryId 类目id
     * @return List<CategoryDTO>
     */
    @RequestMapping("/category/tree")
    public Response getCategoryTree(@RequestParam(required = false) Long categoryId) {
        // 如果categoryId为空，返回所有顶层目录。不为空，返回该目录下的所有子目录。如果目录下有球星，还要把该目录下的球星也返回。
        List<CategoryDTO> subCategoriesByCategoryId = categoryService.getSubCategoriesByCategoryId(categoryId);
        return new Response<>(1001,subCategoriesByCategoryId);
    }
}
